﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using ES_Project.Model;
using System.Web.Configuration;
using System.Data;
namespace ES_Project.Pages
{
    public partial class WebForm31 : System.Web.UI.Page
    {
       

        string StrConn = WebConfigurationManager.ConnectionStrings["essystem"].ToString();
        MySqlCommand objCmd = new MySqlCommand(); 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
            
            }
                //BindData();
                //CMD_INSERT.Enabled = true;
                //CMD_EDIT.Enabled = false;
            
        }
      
        
        
         void BindData()
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            String strSQL;
           
             
            //String strConnString;
            //strConnString = "Server=localhost;User Id=root; Password=root; Database=essystem; Pooling=false";
            //objConn = new MySqlConnection(strConnString);
            //objConn.Open();

            



                //CMD_INSERT.Enabled = false;
                //CMD_EDIT.Enabled = true;
                strSQL = "select crd.cls_reg_detail_id,crd.cls_reg_id,crd.cls_course_no,crd.cls_course_time,crd.cls_course_amt," +
                          "crm.cls_reg_code,crm.cls_name,crm.level " +
                          "from cls_register_detail crd left outer join cls_register_mast crm  " +
                          "on crd.cls_reg_id = crm.cls_reg_id";



                Conn.Open();
                MySqlDataReader dtReader;
                objCmd = new MySqlCommand(strSQL, Conn);
                dtReader = objCmd.ExecuteReader();

                //*** BindData to GridView ***//
                GridView4.DataSource = dtReader;
                GridView4.DataBind();

               // dtReader.Close();
               // dtReader = null;

            
            //using (essystemEntities context = new essystemEntities())
            //{

            //    List<cls_register_detail> mao = context.cls_register_detail.Where("it.[cls_reg_detail_id]=" + ((HiddenField)GridView4.SelectedRow.Cells[1].FindControl("HiddenField3")).Value.ToString() + "").ToList();
            //    if (mao.Count > 0)
            //    {
            //        List<cls_register_detail> mb = context.cls_register_detail.Where("it.[cls_reg_detail_id]=" + ((HiddenField)GridView4.SelectedRow.Cells[1].FindControl("HiddenField3")).Value.ToString() + "").ToList();
            //        if (mb.Count > 0)
            //        {
            //            clsCourseAmt.Text = mb[0].cls_course_amt;
            //            clsCourseTime.Text = mb[0].cls_course_time;
            //            //empNickname.Text = mb[0].emp_nickname;
            //            //empDrgree.Text = mb[0].emp_gender;
            //            clsRegDetailId.Text = mb[0].cls_reg_detail_id.ToString();
            //            //empDegreeSubject.Text = mb[0].emp_degree_subject;
            //            //empInstitution.Text = mb[0].emp_institution;

            //        }
            //    }

            //}

        }
        
        protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                ImageButton ib = (ImageButton)e.Row.FindControl("ImageButton1");
                ib.Attributes.Add("onclick", "return confirm('คุณต้องการที่จะลบข้อมูล?');");

            }
            HyperLink edit = (HyperLink)(e.Row.FindControl("edit"));
            if (edit != null)
            {
                // HyFO4.Text = (string)DataBinder.Eval(e.Row.DataItem, "AgentCode");
                edit.NavigateUrl = "Cost_Register.aspx?clsRegDetailId=" + (string)DataBinder.Eval(e.Row.DataItem, "cls_reg_detail_id").ToString();
                //HyFO4.NavigateUrl = "#";
                //HyFO4.Attributes.Add("Onclick", "opennewsletter('Pay_Course.aspx?emp_id=" + (string)DataBinder.Eval(e.Row.DataItem, "emp_id").ToString() + "');");
                //+ "&TxtAO=" + TxtAO.Text + "&TxtAgent=" + TxtAgent.Text + "&TxtInvoice1=" + TxtInvoice1.Text + "&TxtInvoice2=" + TxtInvoice2.Text + "&TxtDate1=" + TxtDate1.Text + "&TxtDate2=" + TxtDate2.Text + "&StatusWork=" + StatusWork.SelectedValue;
            }
        }
        protected void GridView4_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

            //CMD_INSERT.Enabled = false;
            //CMD_EDIT.Enabled = true;
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;




            strSQL = "select crd.cls_reg_detail_id,crd.cls_reg_id,crd.cls_course_no,crd.cls_course_time,crd.cls_course_amt," +
                          "crm.cls_reg_code,crm.cls_name,crm.level " +
                          "from cls_register_detail crd left outer join cls_register_mast crm  " +
                          "on crd.cls_reg_id = crm.cls_reg_id  " +
                          "WHERE crd.cls_reg_detail_id ='" + GridView4.DataKeys[e.NewSelectedIndex].Value + "'";

            Conn.Open();
            MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds4 = new DataSet();
            da4.Fill(ds4);

            int _row = ds4.Tables[0].Rows.Count;
            int i = 0;
            if (_row > i)
            {

                clsRegDetailId.Text = Convert.ToString(ds4.Tables[0].Rows[i]["cls_reg_detail_id"]).ToString();
                clsRegCode.Text = (string)ds4.Tables[0].Rows[i]["cls_reg_code"].ToString();
                clsName.Text = (string)ds4.Tables[0].Rows[i]["cls_name"].ToString();
                clsCourseTime.Text = (string)ds4.Tables[0].Rows[i]["cls_course_time"].ToString();
                clsCourseAmt.Text = (string)ds4.Tables[0].Rows[i]["cls_course_amt"].ToString();
                clsRegId.Text = (string)ds4.Tables[0].Rows[i]["cls_reg_id"].ToString();
                clsCourseNo.Text = (string)ds4.Tables[0].Rows[i]["cls_course_no"].ToString();
 
            }
        }
        protected void GridView4_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            strSQL = "DELETE FROM cls_register_detail WHERE cls_reg_detail_id ='" + GridView4.DataKeys[e.RowIndex].Value + "'";
            Conn.Open();
            objCmd = new MySqlCommand(strSQL, Conn);

            objCmd.ExecuteNonQuery();


            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert3", "alert('ลบข้อมูลเรียบร้อยแล้ว')", true);
            GridView4.EditIndex = -1;

            //BindData();

        }

        protected void CMD_SEARCH_Click(object sender, EventArgs e)
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            String strSQL;
            String condi = " where 1=1 ";

            if (!clsRegCode.Text.Equals(""))
            {
                condi = condi + " and crm.cls_reg_code = '" + clsRegCode.Text + "'";

            }
            if (!clsName.Text.Equals(""))
            {
                condi = condi + " and crm.cls_name = '" + clsName.Text + "'";

            }
            if (!clsCourseNo.Text.Equals(""))
            {
                condi = condi + " and crd.cls_course_no = '" + clsCourseNo.Text + "'";

            }
            if (!clsCourseTime.Text.Equals(""))
            {
                condi = condi + " and crd.cls_course_time = '" + clsCourseTime.Text + "'";

            }
            if (!clsCourseAmt.Text.Equals(""))
            {
                condi = condi + " and crd.cls_course_amt = '" + clsCourseAmt.Text + "'";

            }


            strSQL = "select crd.cls_reg_detail_id,crd.cls_reg_id,crd.cls_course_no,crd.cls_course_time,crd.cls_course_amt," +
                          "crm.cls_reg_code,crm.cls_name,crm.level " +
                          "from cls_register_detail crd left outer join cls_register_mast crm  " +
                          "on crd.cls_reg_id = crm.cls_reg_id  " + condi + "";
                          //"WHERE crd.cls_reg_detail_id ='" + GridView4.DataKeys[e.NewSelectedIndex].Value + "'";



            Conn.Open();
            MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds = new DataSet();
            da4.Fill(ds, "cls_register_detail");

            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView4.DataSource = ds.Tables["cls_register_detail"];
                GridView4.DataBind();

            }
            else
            {
                GridView4.DataSource = null;
                GridView4.DataBind();
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert1", "alert('ไม่พบข้อมูล')", true);
            }


        }

        protected void CMD_CLEAR_Click(object sender, EventArgs e)
        {
            clear();
            //CMD_INSERT.Enabled = true;
            //CMD_EDIT.Enabled = false;
        }
        void clear()
        {
            clsRegCode.Text = "";
            clsName.Text = "";
            clsCourseAmt.Text = "";
            clsCourseNo.Text = "";
            clsCourseTime.Text = "";
        }

    }
}